﻿

CREATE PROCEDURE [dbo].[proc_Order_Create]
(
	@OId uniqueidentifier
	,@CusId	uniqueidentifier
	,@CusName	nvarchar(50)
	,@CusPerson	nvarchar(50)
	,@CusPersonTel	nvarchar(50)
	,@CusDescription	nvarchar(1000)
	,@CusEstimate	nvarchar(200)
	,@TypeId	int
	,@TypeName	nvarchar(50)
	,@WayId	int
	,@WayName	nvarchar(50)
	,@ProcessId	int
	,@ProcessName	nvarchar(50)
	,@Salesman	nvarchar(50)
	,@SalesmanAccount	nvarchar(50)
	,@TradeDate	datetime
	,@CreateDate	datetime
	,@shjbh	nvarchar(50)
	,@htbh nvarchar(50)
	,@YingShouPrice	decimal(18, 2)
	,@ShiShouPrice	decimal(18, 2)
	,@Discount	nvarchar(50)
	,@PaymentTypeId	int
	,@PaymentType	nvarchar(50)
	,@BankId	int
	,@Bank	nvarchar(50)
	,@PaymentDescription	nvarchar(200)
	,@OtherDescription	nvarchar(1000)
	,@DepId	int
	,@CompanyId	int
	,@StateId int
	,@NOE_Flag int
)
AS
Begin Tran
	Declare @nCount int,@tmpSumJe decimal(18, 2)

	If @CompanyId=0 Or @DepId =0
		Select @Salesman=EName,@DepId=DepartmentId,@CompanyId=CompanyId From Employee Where UserName=@SalesmanAccount
	Else
		Select @Salesman=EName From Employee Where UserName=@SalesmanAccount

	Set @nCount = 0		--初始化变量
	Select @nCount=Count(0),@tmpSumJe=Sum(UnitPrice*Quantity) From Order_Item Where Occupancy=@SalesmanAccount
	If @nCount = 0	--缺少成单项
		Begin
			RollBack Tran
			Return 0
		End

	If @tmpSumJe <> @YingShouPrice	--成单项之和与应收不符
		Begin
			RollBack Tran
			Return 0
		End

	If @NOE_Flag=0	--常规成单
			Insert Into [Order]
			(
				OId
				,CusId
				,CusName
				,CusPerson
				,CusPersonTel
				,CusDescription
				,CusEstimate
				,TypeId
				,TypeName
				,WayId
				,WayName
				,ProcessId
				,ProcessName
				,Salesman
				,SalesmanAccount
				,TradeDate
				,CreateDate
				,YingShouPrice
				,ShiShouPrice
				,Discount
				,PaymentTypeId
				,PaymentType
				,BankId
				,Bank
				,PaymentDescription
				,OtherDescription
				,DepId
				,CompanyId
				,StateId
				,IsGreen
				,shjbh
				,htbh
				,NOE_Flag
				,ExCompanyId
				,F_YingShou	--首次录入时的成单总额，财报统计专用(2012-09-22)
				)
			Values
				(
				@OId
				,@CusId
				,@CusName
				,@CusPerson
				,@CusPersonTel
				,@CusDescription
				,@CusEstimate
				,@TypeId
				,@TypeName
				,@WayId
				,@WayName
				,@ProcessId
				,@ProcessName
				,@Salesman
				,@SalesmanAccount
				,@TradeDate
				,@CreateDate
				,@YingShouPrice
				,@ShiShouPrice
				,@Discount
				,@PaymentTypeId
				,@PaymentType
				,@BankId
				,@Bank
				,@PaymentDescription
				,@OtherDescription
				,@DepId
				,@CompanyId
				,@StateId
				,0
				,@shjbh
				,@htbh
				,@NOE_Flag
				,@CompanyId
				,@YingShouPrice	--首次录入时的成单总额，财报统计专用(2012-09-22)
				)
	Else	--非常规(含非企)成单
		Insert Into [Order]
		(
			OId
			,CusId
			,CusName
			,CusPerson
			,CusPersonTel
			,CusDescription
			,CusEstimate
			,TypeId
			,TypeName
			,WayId
			,WayName
			,ProcessId
			,ProcessName
			,Salesman
			,SalesmanAccount
			,TradeDate
			,CreateDate
			,YingShouPrice
			,ShiShouPrice
			,Discount
			,PaymentTypeId
			,PaymentType
			,BankId
			,Bank
			,PaymentDescription
			,OtherDescription
			,DepId
			,CompanyId
			,StateId
			,IsGreen
			,TrackKfAccount
			,TrackKfName
			,TrackKfDepId
			,shjbh
			,htbh
			,NOE_Flag
			,ExCompanyId
			,F_YingShou	--首次录入时的成单总额，财报统计专用(2012-09-22)
		)
		Values
		(
			@OId
			,@CusId
			,@CusName
			,@CusPerson
			,@CusPersonTel
			,@CusDescription
			,@CusEstimate
			,@TypeId
			,@TypeName
			,@WayId
			,@WayName
			,@ProcessId
			,@ProcessName
			,@Salesman
			,@SalesmanAccount
			,@TradeDate
			,@CreateDate
			,@YingShouPrice
			,@ShiShouPrice
			,@Discount
			,@PaymentTypeId
			,@PaymentType
			,@BankId
			,@Bank
			,@PaymentDescription
			,@OtherDescription
			,@DepId
			,@CompanyId
			,@StateId
			,0
			,@SalesmanAccount
			,@Salesman
			,@DepId
			,@shjbh
			,@htbh
			,@NOE_Flag
			,@CompanyId
			,@YingShouPrice	--首次录入时的成单总额，财报统计专用(2012-09-22)
		)

	Insert Into Order_PaymentRecord
	(
		OId
		,CusId
		,CusName
		,SalesmanAccount
		,Salesman
		,CreateDate
		,Prices
		,PaymentTypeId
		,PaymentType
		,BankId
		,Bank
		,SWId
		,SWName
		,FinanceFlag
		,CompanyId
	)
	Values
	(
		@OId
		,@CusId
		,@CusName
		,@SalesmanAccount
		,@Salesman
		,@CreateDate
		,@ShiShouPrice
		,@PaymentTypeId
		,@PaymentType
		,@BankId
		,@Bank
		,1
		,'首款'
		,0
		,@CompanyId
	)

	Update Order_Item Set OId=@OId,Occupancy=NULL,CreateDate=Getdate(),TradeDate=@TradeDate Where Occupancy=@SalesmanAccount
	Update Customer Set StateId=2,ProtectId=3,ProtectName=NULL,ProtectDate=NULL Where CustomerId=@CusId

	If @@error <> 0  --发生错误
		Begin
			RollBack Tran
			Return 0
		End
	Else
		Begin
			Commit Tran
			Return 1	--执行成功
End